// packageC/member/visit/Inviter/Inviter.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    name: "",
    img: "",
    title: "",
    fromGood: '',
    is_bind_invite: 0,
    lk:0,
    parent_id:''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.scene) {
        let scene = decodeURIComponent(options.scene);
        if (scene) {
          var info_arr = [];
          info_arr = scene.split("&");
          for (let i = 0; i < info_arr.length; i++) {
            let chil_arr = [];
            chil_arr = info_arr[i].split("=");
            if (chil_arr[0] == "m"){
              app._setMid(chil_arr[1])
            }else if (chil_arr[0] == "lk") {
              this.setData({
                lk: chil_arr[1],
              });
            }
          }
        }
      } 
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this.getUserinfo();
    this.showName();
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  getUserinfo() {
    let that = this;
    let mid = '';
    try {
      var value = wx.getStorageSync('mid');
      if (value) {
        mid = value;
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
    let urlStr = app.getNetAddresss("member.member.getUserInfo");
    app._getNetWork({
      url: urlStr,
      data: {
        mid: mid
      },
      validateBol:2,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            parent_id:res.data.yz_member.parent_id,
            name: res.data.referral.nickname,
            img: res.data.referral.avatar,
            is_bind_invite: res.data.is_bind_invite,
            inviteCode:res.data.inviteCode
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  showName() {
    let that = this;
    let urlStr = app.getNetAddresss("member.member.getShopSet");
    app._getNetWork({
      url: urlStr,
      data: {},
      validateBol:2,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            title: res.data.shop_set_name,
            default_invite: res.data.default_invite
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  affirm() {
    try {
      wx.setStorageSync('isset', 1);
    } catch (e) {
      console.log(e);
    }
    if (this.data.fromGood == 1) {
      //商品下单判断是否需要邀请码
      let urlStr = app.getNetAddresss("member.member.confirmGoods");
      app._getNetWork({
        url: urlStr,
        data: {},
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.navigateBack({
              delta: 1
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    } else {

      app._getNetWork({
        url:app.getNetAddresss("member.member.update-member-invite"),
        data:{parent_id:this.data.parent_id},
        success:resdata=>{
          if(resdata.data.result==1){

            if(this.data.lk==1){
              wx.navigateTo({
                url: '/packageF/sign_in_subscribe/index/index',
              })
              return;
            }

            wx.navigateTo({
              url: '/packageA/member/editmobile/editmobile?fromApp=3&code='+this.data.inviteCode
            });
          }else{
            app.tips(resdata.data.msg)
          }
        }
      })



      //商城登录输入邀请码
   
    }
  },

});
